package com.zm.zmgz.controller;

import com.zm.zmgz.annotationzm.ButtonAuthorityAnnotation;
import com.zm.zmgz.annotationzm.LoginRequired;
import com.zm.zmgz.pojo.AuthorityData2Bean;
import com.zm.zmgz.pojo.Response;
import com.zm.zmgz.pojo.UserBean;
import com.zm.zmgz.service.AuthorityService;
import com.zm.zmgz.service.UserService;
import com.zm.zmgz.utils.ConfigureBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@RestController
@RequestMapping("/user")
public class UserController { // 该控制层包含了用户功能和用户模块功能



    @Autowired
    UserService userService;

    @Autowired
    AuthorityService authorityService;

    @Autowired
    ConfigureBean configureBean;

    @PostMapping("/hello3") // 测试方法
    public String hello3(@RequestParam String name, @RequestParam Integer age) {//传入账号名和密码,在servletContext中存储该用户对象并返回用户对象
        System.err.println("name:"+name);
        System.err.println("age:"+age);
        return "hllo3";
    }

    @GetMapping("/hello2") // 测试方法
    public String hello2() {//传入账号名和密码,在servletContext中存储该用户对象并返回用户对象
        return "hello!";
    }

    @GetMapping("/hello") // 测试方法
    public String hello() {//传入账号名和密码,在servletContext中存储该用户对象并返回用户对象
        Thread thread = Thread.currentThread();
        return "hello" + ":" + thread.getName();
    }

    @PostMapping("/login") // 用户登录
    public Response login(@RequestBody UserBean userBean, HttpServletRequest request) {//传入账号名和密码,在servletContext中存储该用户对象并返回用户对象
        System.out.println(userBean);
        UserBean userbean2 = userService.getUserByUnameAndPassword(userBean.getUsername(), userBean.getPassword());
        System.out.println("userbean2:" + userbean2);
        if (userbean2.getUsername() == null)
            return new Response(20000, "账号或密码错误!", null);
        userbean2.setAuthorityBeanList(authorityService.getAuthoritys(userbean2.getIdentificationnumber())); // 设置权限列表
        // 添加userBean到内置对象application中,key为Identificationnumber
        ServletContext servletContext = request.getServletContext();
        HttpSession session = request.getSession();
        String id = session.getId();
        System.out.println("id登录:" + id);
//        String requestedSessionId = session.getId();
//        request.ses
//        System.out.println("getRequestedSessionId:"+requestedSessionId);
        // 避免一个用户再两处登录
        servletContext.setAttribute(userbean2.getIdentificationnumber(), id); // 标志该身份证用户以登录,并且记录登录的sessionID <身份证,sessionID>
        servletContext.setAttribute(id, userbean2); // <sessionID,用户对象>
        userbean2.setToken(userbean2.getIdentificationnumber());
        if (userbean2.getAuthorityBeanList().size() < 1) { // 无任何权限
            return new Response(20000, userbean2.getUsername() + userbean2.getUsername() + "账号无任何权限!", userbean2);
        }
        return userbean2.getStatus() == 1 ? new Response(20000, userbean2.getUsername() + "账号已被停用!", userbean2) : new Response(20000, "登陆成功!", userbean2);
//        return userbean2;
    }

    //    @LoginRequired // 需要登录
    @PostMapping("/logout") // 用户退出登录
    public Response logout(@RequestHeader("X-Token") String token, HttpServletRequest request) { // 不加@RequestBody,token取不到值
        ServletContext servletContext = request.getServletContext();
        if (!Objects.isNull(servletContext.getAttribute(token))) { // 清除合法sessionID和合法sessionID对应的数据对象
            // 如果没有找到,说明该用户并没有登录
            servletContext.setAttribute(token, null); // 清除合法sessionID
            servletContext.setAttribute(request.getRequestedSessionId(), null); // 合法sessionID对应的数据对象
        }
        return new Response(20000, "您已退出登录!");
    }

    @LoginRequired // 需要登录
    @GetMapping("/info")
    public UserBean getInfo(@RequestHeader("X-Token") String token, HttpServletRequest request) { // 根据传入的token值,返回对应的userbean对象
        ServletContext servletContext = request.getServletContext();
        if (Objects.isNull(servletContext.getAttribute(request.getSession().getId()))) { // 如果没有找到,返回一个空userbean
            UserBean userBean = new UserBean();
            userBean.setCode(50008); // 50008表示传入的token是一个非法token,请重新登录
            return userBean;
        } else {
            return (UserBean) servletContext.getAttribute(request.getSession().getId());
        }
    }

    public UserController() {
    }

    @LoginRequired // 需要登录
    @GetMapping("/getUsers")
    public Response getUsers() { // 获取所有的用户对象
        return new Response(20000, "获取所有用户对象成功!", userService.getUsers());
    }

    @LoginRequired // 需要登录
    @GetMapping("/getAvailabledentificationnumbers")
    public Response getAvailabledentificationnumbers() { // 注册用户时,获取可用身份证列表
        List<Map<String, String>> mapList = userService.getAvailabledentificationnumbers();
        return new Response(20000, "获取可用身份证列表成功!", mapList);
    }

    /*
     * 功能:增加1个用户
     *
     * */
    @LoginRequired
    @PostMapping("/addUser")
    @ButtonAuthorityAnnotation("/user/management/add")
    public Response addUser(@RequestHeader("X-Token") String token, @RequestBody UserBean userBean) {
        System.out.println(userBean);
        int count = userService.addUser(userBean);
        return count == 1 ? new Response(20000, "用户添加成功!", count) : new Response(20000, "用户添加失败!", count);
    }


    /*
     * 功能:修改1个用户的信息
     *
     * */
    @LoginRequired
    @PutMapping("/editUser")
    @ButtonAuthorityAnnotation("/user/management/edit")
    public Response editUser(@RequestHeader("X-Token") String token, @RequestBody UserBean userBean, HttpServletRequest request) {
        System.out.println("employeeBean:" + userBean.toString());
        int count = userService.editUser(userBean);
        if (userBean.getStatus() == 1) { // 如果该用户被设置为停用,使该用户退出登录
            System.out.println("用户" + userBean.getIdentificationnumber() + "已经强制退出!");
            ServletContext servletContext = request.getServletContext();
            servletContext.setAttribute(userBean.getIdentificationnumber(), null);
        }
        return count == 1 ? new Response(20000, "用户信息修改成功!", count) : new Response(20000, "用户信息修改失败!", count);
    }

    /*
     * 功能:删除1个用户
     *
     * */
    @LoginRequired
    @DeleteMapping("/deleteUser")
    @ButtonAuthorityAnnotation("/user/management/delete")
    public Response deleteEmployee(@RequestHeader("X-Token") String token, @RequestBody UserBean userBean) {
        System.out.println("UserBean:" + userBean.toString());
        int count = userService.deleteUser(userBean);
        return count == 1 ? new Response(20000, "用户删除成功!", count) : new Response(20000, "用户删除失败!", count);
    }

    /*
     * 功能:获取权限信息
     *
     * */
    @LoginRequired
    @GetMapping("/getAuthorityData2")
    public Response getAuthorityData2(String identificationnumber) {
        System.out.println("identificationNumber:" + identificationnumber);
        AuthorityData2Bean authorityData2Bean = userService.getAuthorityData2(identificationnumber);
        return new Response(20000, "权限获取成功!", authorityData2Bean);
    }

    /*
     * 功能:设置权限信息
     *
     * */
    @LoginRequired
    @PutMapping("/editAuthorityData2")
    @ButtonAuthorityAnnotation("/user/management/authority")
    public Response editAuthorityData2(@RequestHeader("X-Token") String token, @RequestBody AuthorityData2Bean authorityData2Bean, HttpServletRequest request) {
//        System.out.println("AuthorityData2Bean:"+authorityData2Bean);
        Boolean aBoolean = userService.editAuthorityData2(authorityData2Bean);
        ServletContext servletContext = request.getServletContext();
        String s = (String) servletContext.getAttribute(authorityData2Bean.getIdentificationnumber()); // sessionID
//        System.out.println("编辑:"+attribute.toString());
        if (!Objects.isNull(s)) {
            servletContext.setAttribute(authorityData2Bean.getIdentificationnumber(), null);
            System.out.println("用户" + authorityData2Bean.getIdentificationnumber() + "以强制退出登录!");
        }
        return aBoolean == true ? new Response(20000, "权限设置成功!", aBoolean) : new Response(20000, "权限设置失败!", aBoolean);
    }

}
